;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
;;; Copyright © 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
;;; Copyright © 2017 Jan Nieuwenhuizen <janneke@gnu.org>
;;; Copyright © 2018, 2019 Ricardo Wurmus <rekado@elephly.net>
;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guile-web)
  #:use-module (guix gexp)
  #:use-module (guix records)
  #:use-module (gnu packages admin)
  ;; #:autoload   (gnu packages ci) (cuirass)
  ;; #:autoload   (gnu packages version-control) (git)
  #:use-module (gnu services)
  #:use-module (gnu services base)
  #:use-module (gnu services shepherd)
  #:use-module (gnu services admin)
  #:use-module (gnu system shadow)
  #:export (<guile-web-configuration>
            guile-web-configuration
            guile-web-configuration?
            guile-web-service-type
            ;; a user on irc said I probably do not need to export the
            ;; user or group.
            ;;user
            ;;group
            ))

;;;; Commentary:
;;; This file provides a shepherd service to run my homebrewed version
;;; of a website.  ~/prog/guile/web/web.scm
;;;; Code:

(define-record-type* <guile-web-configuration>
  guile-web-configuration make-guile-web-configuration
  guile-web-configuration?
  (guile guile-web-configuration-guile ;;package:
         (default guile)) ;;guile-2.0
  (user  guile-web-configuration-user ;string
         (default "joshua"))
  (group guile-web-configuration-group ;string
         (default "joshua")))

(define (guile-web-shepherd-service config)
  "Return a <shepherd-service> for the Guile-Web service."
  (let ((guile guile-web-configuration-guile)
        (user  guile-web-configuration-user)
        (group guile-web-configuration-group))
    (list (shepherd-service
           (documentation "Run the guile web site that I made.")
           (provision '(guile-web))
           (requirement '(networking))
           (start #~(make-forkexec-constructor
                     (list (string-append #$guile "/bin/guile")
                           "/home/joshua/prog/guile/web/web.scm")
                     ;; #:environment-variables
                     ;; (list "GPG_TTY=$(tty)")
                     #:user #$user
                     #:group #$group
                     ))
           (stop #~(make-kill-destructor))))))

;; <leoprikler> guile-web-service-type should extend shepherd-service,
;;passing guile-web-shepherd service <leoprikler> inside your shepherd service check whether the field names are
;;actually correct  [18:49]

(define-public guile-web-service-type
  (service-type
   (name 'guile-web)
   (description "Run the program ~/prog/guile/web.scm")
   (extensions
    (list
     ;; (service-extension profile-service-type      ;for 'info guile'
     ;;                    (compose list guile-web-configuration-guile))
     (service-extension shepherd-service guile-web-shepherd-service)
     ;; I could also use shepherd-root-service-type  ???
     ;;(service-extension shepherd-service guile-web-service)
     )
    )
   (default-value (guile-web-configuration))
   ))

(display "I am displaying guile web service type\n")
(display guile-web-service-type)
(display "\n")
